package com.example.provider.db.mysql.repository

import com.example.provider.db.mysql.model.Customer
import org.springframework.data.jdbc.repository.query.Modifying
import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.repository.CrudRepository
import org.springframework.data.repository.query.Param
import java.util.*


interface CustomerRepository  :CrudRepository<Customer, Long>{

    @Query("""
           SELECT *
           FROM Customers c 
           WHERE (:name IS NULL OR c.name = :name)
             AND (:email IS NULL OR c.email = :email)
             AND c.status = 0
       """)
    fun findCustomers(
        @Param("name") name: String?,
        @Param("email") email: String?
    ): List<Customer>

    @Modifying
    @Query("UPDATE Customers c SET c.status = 1 WHERE c.id = :id")
    override fun deleteById(id: Long)

    @Query("select * from Customers c where  c.status = 0 and c.id=:id")
    fun get( @Param("id") id: Long): Optional<Customer>

    @Query("select * from Customers c where  c.status = 0 and c.email=:email")
    fun getByEmail( @Param("email") email: String?): Optional<Customer>
}